package cn.tedu.system.oa.user.service.impl;

import cn.tedu.system.oa.base.exception.ServiceException;
import cn.tedu.system.oa.base.response.StatusCode;
import cn.tedu.system.oa.dept.pojo.dto.DeptQuery;
import cn.tedu.system.oa.dept.pojo.entity.Dept;
import cn.tedu.system.oa.dept.service.DeptService;
import cn.tedu.system.oa.user.mapper.UserMapper;
import cn.tedu.system.oa.user.pojo.dto.UserLoginParam;
import cn.tedu.system.oa.user.pojo.dto.UserQuery;
import cn.tedu.system.oa.user.pojo.dto.UserSaveParam;
import cn.tedu.system.oa.user.pojo.entity.User;
import cn.tedu.system.oa.user.pojo.vo.UserVO;
import cn.tedu.system.oa.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    private DeptService deptService;

    @Override
    public UserVO userLogin(UserLoginParam userLoginParam) {
        log.debug("用户登录业务:参数={}",userLoginParam);
        UserVO userVO = userMapper.userLogin(userLoginParam.getUsername()); //根据用户名查询该用户是否存在
        if(userVO == null){ //表示不存在，抛出业务层异常
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        //表示存在但密码错误，同样抛出业务层异常
        if(!userLoginParam.getPassword().equals(userVO.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("加载用户列表业务:参数={}",userQuery);
        if(userQuery.getDeptName() != null && userQuery.getDeptName().trim().length() != 0){
            Dept dept = selectDept(null, userQuery.getDeptName());
            userQuery.setDeptId(dept.getId());
        }
        List<UserVO> list = userMapper.selectUser(userQuery);
        for (UserVO userVO : list) {
            Integer parentId = userVO.getParentId();
            if(parentId != null){
                UserQuery query = new UserQuery();
                query.setId(parentId);
                List<UserVO> userVOS = userMapper.selectUser(query);
                userVO.setParentName(userVOS.get(0).getUsername());
            }
        }
        return list;
    }

    @Override
    public void saveUser(@RequestBody UserSaveParam userSaveParam) {
        log.debug("保存用户业务:参数={}",userSaveParam);
        Dept dept = selectDept(userSaveParam.getParentId(), null);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId() == null){ //无id表示新增用户
            user.setDeptId(dept.getId()); //设置部门id
            user.setCreateTime(new Date());
            //user.setPassword("123456"); //设置初始密码
            userMapper.insert(user);
        }else { //有id表示修改用户信息
            user.setDeptId(dept.getId()); //设置部门id
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void resetPwd(Integer userId) {
        log.debug("重置密码功能:参数={}",userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Integer userId) {
        log.debug("删除用户功能:参数={}",userId);
        userMapper.deleteUser(userId);
    }

    @Override
    public List<UserVO> selectUserLevel() {
        return userMapper.selectByLevel();
    }

    //根据用户id和部门名称查询对应部门
    public Dept selectDept(Integer userId,String deptName){
        DeptQuery deptQuery = new DeptQuery();
        deptQuery.setUserId(userId);
        deptQuery.setDeptName(deptName);
        List<Dept> deptList = deptService.selectDept(deptQuery);
        return deptList.get(0);
    }
}
